$(function () {
    /*  选择全部checkbox*/
     $('input[name=checkbox_all]').on('ifChecked', function(event){
      $('input').iCheck('check');
    });
     $('input[name=checkbox_all]').on('ifUnchecked', function(event){
      $('input').iCheck('uncheck');
    });
     /*delete 是否可选择？*/
     $('input').on('ifChecked', function(event){
      $('.checkbox-opreat').removeClass('disabled');
    });
     $('input').on('ifUnchecked', function(event){
      ifDeletBtn();
    });
});
function getDbids(){
  var arrId=new Array();
  $('input[type=checkbox]:checked').each(function(num){
      var id = $(this).val();
      if(id!=null&&id!=""){
        rid = id.replace("/&code=/","|");
        arrId.push(rid);
      }
  });
  return arrId.join(",");
}

/*  统计当前所有checkbox选中的个数:如果为0 则delete按钮disabled，*/
function ifDeletBtn(){
   var checkedNum = $('input[type=checkbox]:checked').size();
   if(checkedNum<=0){
         $('.checkbox-opreat').addClass('disabled');
    }else{
      $('.checkbox-opreat').removeClass('disabled');
    }
}


//验证是否有效的Url return true/false
function IsURL(str_url) { 
  var strRegex="^((https|http|ftp|rtsp|mms)?://)"
    + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" // ftp的user@
    + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
    + "|" // 允许IP和DOMAIN（域名）
    + "([0-9a-z_!~*'()-]+\.)*" // 域名- www.
    + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名
    + "[a-z]{2,6})" // first level domain- .com or .museum
    + "(:[0-9]{1,4})?" // 端口- :80
    + "((/?)|" // a slash isn't required if there is no file name
    + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
  var re=new RegExp(strRegex); 
  return re.test(str_url);
}

/* 弹窗提示 信息*/
function changeDialog(url,title){  /* 批量删除及操作*/
  $.Zebra_Dialog('<strong>'+title+'</strong>', {
      'type':     'question',
      'title':    '提示',
      'buttons': [
                    {caption: 'Yes', callback: function() {
                      $.ajax({
                         url:url,
                         type:"GET",
                         dataType:'json',
                         success:function(data){
                            showDialog(data.status,data.message);
                          },
                         error:function(e){
                            showDialog(0,"数据错误");
                          }
                      });

                    }},
                    {caption: 'No', callback: function() {}},
                  ],
      'animation_speed_hide':0,
  });
}

/* 弹窗提示 信息*/
function checkChanges(url,title){  /* 批量删除及操作*/
  $.Zebra_Dialog('<strong>'+title+'</strong>', {
      'type':     'question',
      'title':    '提示',
      'buttons': [
                    {caption: 'Yes', callback: function() {
                      var ids = getDbids(); 
                      $.ajax({
                         url:url+"&ids="+ids,
                         type:"GET",
                         dataType:'json',
                         success:function(data){
                            showDialog(data.status,data.message);
                          },
                         error:function(e){
                            showDialog(0,"数据错误");
                          }
                      });

                    }},
                    {caption: 'No', callback: function() {}},
                  ],
      'animation_speed_hide':0,
  });
}




function checkDeletes(url,title){  /* 批量删除及操作*/
  $.Zebra_Dialog('<strong>'+title+'</strong>', {
      'type':     'question',
      'title':    '提示',
      'buttons': [
                    {caption: 'Yes', callback: function() {
                      var dbids = getDbids(); 
                      $.ajax({
                         url:url+"?ids="+dbids,
                         type:"GET",
                         dataType:'json',
                         success:function(data){
                            showDialog(data.status,data.message);
                          },
                         error:function(e){
                            showDialog(0,"数据错误");
                          }
                      });

                    }},
                    {caption: 'No', callback: function() {}},
                  ],
      'animation_speed_hide':0,
  });
}
function checkDialog(url,title){  /* 单个修改及操作*/
  $.Zebra_Dialog('<strong>'+title+'</strong>', {
      'type':     'question',
      'title':    '提示',
      'buttons': [
                    {caption: 'Yes', callback: function() {
                      $.ajax({
                         url:url,
                         type:"GET",
                         dataType:'json',
                         success:function(data){
                            showDialog(data.status,data.message);
                          },
                         error:function(e){
                            showDialog(0,"数据错误");
                          }
                      });

                    }},
                    {caption: 'No', callback: function() {}},
                  ],
      'animation_speed_hide':0,
  });
}

function listorder(url,title){  /* 排序操作*/
  $.Zebra_Dialog('<strong>'+title+'</strong>', {
      'type':     'question',
      'title':    '提示',
      'buttons': [
                    {caption: 'Yes', callback: function() {
                      var formdata= $("#form-listorder").serializeArray();
                      $.ajax({
                         url:url,
                         type:"POST",
                         data:formdata,
                         dataType:'json',
                         success:function(data){
                            showDialog(data.status,data.message);
                          },
                         error:function(e){
                            showDialog(0,"数据错误");
                          }
                      });

                    }},
                    {caption: 'No', callback: function() {}},
                  ],
      'animation_speed_hide':0,
  });
}
function showDialog(status,message){
  if(status){
     new $.Zebra_Dialog('<strong>'+message+'</strong>',
     {
        'type':     'information',
        'title':    '提示',
        'animation_speed_hide':250,
        'auto_close':10000,
        'onClose':function(){
            window.location.reload();
        }
      });
  }else{
       new $.Zebra_Dialog('<strong>'+message+'</strong>',
       {
          'type':     'error',
          'title':    '提示',
          'animation_speed_hide':250,
          'auto_close':100000,
        });
  }      
}
function resteForm(href){
   $('#form-seearch')[0].reset();
  window.location.href=href;
}
/*输入自动提示并完成*/
function autoComplete(objId,keywords,url,color){
  keywords = keywords.replace(/[ ]/g,"")
    $.ajax({
       url:url,
       type:"POST",
       data:{keywords:keywords},
       dataType:'json',
       success:function(data){
          if (data.status) {
            var dataArr=data.dataValue;
            
            showAutoCompleteSelect(dataArr,objId,keywords,color);
          };
        },
       error:function(e){
          //showDialog(0,"数据错误");
        }
    }); 
}
function showAutoCompleteSelect(dataArr,objId,keywords,color)
{
  if ($('#'+objId+"-option").length > 0) {
    console.info("#"+objId+"-option存在");

  }else{


    var selectStr="<div class='auto-complete-select'><ul class='auto-complete-select-option' id='"+objId+"-option'></ul></div>";
    $('#'+objId).parent('div').append(selectStr);
  }  
  if (dataArr!=null) 
  { 
    var optionStr="";
      for (var i = 0; i <dataArr.length ; i++) {
        var colorStr="<span style='color:"+color+"'>"+keywords+"</span>";  
        var objValue =dataArr[i]['company']
        dataArr[i]['company']=dataArr[i]['company'].replace(keywords,colorStr); 
        dataArr[i]['contact']=dataArr[i]['contact'].replace(keywords, colorStr); 
        dataArr[i]['tell']=dataArr[i]['tell'].replace(keywords, colorStr); 
        optionStr+="<li><a href='javascript:void(0)' onclick=\"autoCompleteInput('"+objValue+"','"+objId+"')\">"+dataArr[i]['company']+","+dataArr[i]['contact']+","+dataArr[i]['tell']+"</a></li>";
      };
      $('#'+objId+'-option').html(optionStr);
  }
}
function autoCompleteInput(objValue,objId)
{
  $('#'+objId).val(objValue);
  $('#'+objId+'-option').html("");
  $('#'+objId).parent('div').children('.auto-complete-select').remove();
}

